package com.tpc.control.jpa;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * ReserveItemLab entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "reserve_item_lab", catalog = "production_control")
public class ReserveItemLab implements java.io.Serializable {

	// Fields

	private ReserveItemLabId id;
	private ReserveItem reserveItem;
	private Date reserveLabDate;
	private String remark;
	private Date confirmLabPlan;
	private Date confirmLabActual;
	private String colorTpc;
	private String colorCustomer;
	private String detail;
	private String noLab;
	private Date standardDate;
	private Timestamp createDate;
	private String createBy;
	private Set<ReserveItemLabAnswer> reserveItemLabAnswers = new HashSet<ReserveItemLabAnswer>(
			0);

	// Constructors

	/** default constructor */
	public ReserveItemLab() {
	}

	/** minimal constructor */
	public ReserveItemLab(ReserveItemLabId id, ReserveItem reserveItem,
			Timestamp createDate, String createBy) {
		this.id = id;
		this.reserveItem = reserveItem;
		this.createDate = createDate;
		this.createBy = createBy;
	}

	/** full constructor */
	public ReserveItemLab(ReserveItemLabId id, ReserveItem reserveItem,
			Date reserveLabDate, String remark, Date confirmLabPlan,
			Date confirmLabActual, String colorTpc, String colorCustomer,
			String detail, String noLab, Date standardDate,
			Timestamp createDate, String createBy,
			Set<ReserveItemLabAnswer> reserveItemLabAnswers) {
		this.id = id;
		this.reserveItem = reserveItem;
		this.reserveLabDate = reserveLabDate;
		this.remark = remark;
		this.confirmLabPlan = confirmLabPlan;
		this.confirmLabActual = confirmLabActual;
		this.colorTpc = colorTpc;
		this.colorCustomer = colorCustomer;
		this.detail = detail;
		this.noLab = noLab;
		this.standardDate = standardDate;
		this.createDate = createDate;
		this.createBy = createBy;
		this.reserveItemLabAnswers = reserveItemLabAnswers;
	}

	// Property accessors
	@EmbeddedId
	@AttributeOverrides( {
			@AttributeOverride(name = "reserveNo", column = @Column(name = "reserve_no", nullable = false, length = 20)),
			@AttributeOverride(name = "reserveLine", column = @Column(name = "reserve_line", nullable = false)),
			@AttributeOverride(name = "reserveLabNo", column = @Column(name = "reserve_lab_no", nullable = false)) })
	public ReserveItemLabId getId() {
		return this.id;
	}

	public void setId(ReserveItemLabId id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns( {
			@JoinColumn(name = "reserve_no", referencedColumnName = "reserve_no", nullable = false, insertable = false, updatable = false),
			@JoinColumn(name = "reserve_line", referencedColumnName = "reserve_line", nullable = false, insertable = false, updatable = false) })
	public ReserveItem getReserveItem() {
		return this.reserveItem;
	}

	public void setReserveItem(ReserveItem reserveItem) {
		this.reserveItem = reserveItem;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "reserve_lab_date", length = 10)
	public Date getReserveLabDate() {
		return this.reserveLabDate;
	}

	public void setReserveLabDate(Date reserveLabDate) {
		this.reserveLabDate = reserveLabDate;
	}

	@Column(name = "remark", length = 65535)
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "confirm_lab_plan", length = 10)
	public Date getConfirmLabPlan() {
		return this.confirmLabPlan;
	}

	public void setConfirmLabPlan(Date confirmLabPlan) {
		this.confirmLabPlan = confirmLabPlan;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "confirm_lab_actual", length = 10)
	public Date getConfirmLabActual() {
		return this.confirmLabActual;
	}

	public void setConfirmLabActual(Date confirmLabActual) {
		this.confirmLabActual = confirmLabActual;
	}

	@Column(name = "color_tpc", length = 10)
	public String getColorTpc() {
		return this.colorTpc;
	}

	public void setColorTpc(String colorTpc) {
		this.colorTpc = colorTpc;
	}

	@Column(name = "color_customer", length = 10)
	public String getColorCustomer() {
		return this.colorCustomer;
	}

	public void setColorCustomer(String colorCustomer) {
		this.colorCustomer = colorCustomer;
	}

	@Column(name = "detail", length = 16)
	public String getDetail() {
		return this.detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	@Column(name = "no_lab", length = 10)
	public String getNoLab() {
		return this.noLab;
	}

	public void setNoLab(String noLab) {
		this.noLab = noLab;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "standard_date", length = 10)
	public Date getStandardDate() {
		return this.standardDate;
	}

	public void setStandardDate(Date standardDate) {
		this.standardDate = standardDate;
	}

	@Column(name = "create_date", nullable = false, length = 19)
	public Timestamp getCreateDate() {
		return this.createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	@Column(name = "create_by", nullable = false, length = 20)
	public String getCreateBy() {
		return this.createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "reserveItemLab")
	public Set<ReserveItemLabAnswer> getReserveItemLabAnswers() {
		return this.reserveItemLabAnswers;
	}

	public void setReserveItemLabAnswers(
			Set<ReserveItemLabAnswer> reserveItemLabAnswers) {
		this.reserveItemLabAnswers = reserveItemLabAnswers;
	}

	/* Customize */
	private class ItemLabAnswerSortByNo implements Comparator<ReserveItemLabAnswer>{
	    public int compare(ReserveItemLabAnswer o1, ReserveItemLabAnswer o2) {
	        return o1.getId().getReserveLabAnswerNo().compareTo(o2.getId().getReserveLabAnswerNo());
	    }
	}
	
	public List<ReserveItemLabAnswer> retrieveReserveItemLabAnswersSort() {
		List<ReserveItemLabAnswer> dataList = new ArrayList<ReserveItemLabAnswer>(this.getReserveItemLabAnswers());
		Collections.sort(dataList, new ItemLabAnswerSortByNo());
		return dataList;
	}
}